/*
 * Copyright 2012-2021 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.boot.web.reactive.error;

import java.util.Collections;
import java.util.Map;

import org.springframework.boot.web.error.ErrorAttributeOptions;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.server.ServerWebExchange;

/**
 * 提供访问错误属性的接口，这些属性可以用于日志记录或向用户展示。
 *
 * @author Brian Clozel
 * @author Scott Frederick
 * @since 2.0.0
 * @see DefaultErrorAttributes
 */
public interface ErrorAttributes {

	/**
	 * {@link ServerRequest#attribute(String)} 请求属性的名称，
	 * 用于保存由 {@code ErrorAttributes} 实现解析出的错误信息。
	 * @since 2.5.0
	 */
	String ERROR_ATTRIBUTE = ErrorAttributes.class.getName() + ".error";

	/**
	 * 返回错误属性的 {@link Map}。该 Map 可以作为错误页面的模型，
	 * 或作为 {@link ServerResponse} 的响应体返回。
	 * @param request 源请求
	 * @param options 错误属性内容的选项
	 * @return 错误属性的映射
	 */
	default Map<String, Object> getErrorAttributes(ServerRequest request, ErrorAttributeOptions options) {
		return Collections.emptyMap();
	}

	/**
	 * 返回错误的根本原因异常，如果无法提取错误则返回 {@code null}。
	 * @param request 源 {@link ServerRequest}
	 * @return 导致错误的 {@link Exception} 或 {@code null}
	 */
	Throwable getError(ServerRequest request);

	/**
	 * 在当前 {@link ServerWebExchange} 中存储给定的错误信息。
	 * @param error 导致错误的异常
	 * @param exchange 源交换对象
	 */
	void storeErrorInformation(Throwable error, ServerWebExchange exchange);

}
